package com.yuyu.shop.infrastructure.persistence.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.yuyu.commons.constant.BaseConstant;
import com.yuyu.shop.domain.goods.DO.GoodsDO;
import com.yuyu.shop.domain.goods.repository.GoodsRepository;
import com.yuyu.shop.infrastructure.persistence.convert.GoodsPoConvert;
import com.yuyu.shop.infrastructure.persistence.mapper.GoodsMapper;
import com.yuyu.shop.infrastructure.persistence.po.GoodsPO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * @author
 * @version 1.0
 * @description: TODO
 * @date 2022/8/4 17:37
 */
@RequiredArgsConstructor(onConstructor = @_(@Autowired))//不需要加注解注入
@Service
public class GoodsRepositoryImpl implements GoodsRepository {

    private final GoodsMapper goodsMapper;


    Cache<String,GoodsDO> build = Caffeine.newBuilder().maximumSize(100L)
            .expireAfterAccess(10L, TimeUnit.SECONDS).build();

    @Override
    public GoodsDO getById(Long id) {
            return GoodsPoConvert.PO2DO(goodsMapper.selectOne(
                    new LambdaQueryWrapper<GoodsPO>().eq(GoodsPO::getGoodsId, id).eq(GoodsPO::getIsDel, BaseConstant.ONE)));
    }

    @Override
    public List<GoodsDO> getAll() {
        return goodsMapper.selectList(null).stream().map(GoodsPoConvert::PO2DO).collect(Collectors.toList());
    }
}
